package com.qingxin.sms.service;

import java.util.Date;

import org.springframework.stereotype.Service;

import com.qingxin.sms.entity.SmsAuthcode;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;

@Service
public class SmsAuthcodeService extends BaseService<SmsAuthcode, String> {

	
	
	/**
	 * 添加
	 * @param authcode
	 * @return
	 */
	public SysError add(SmsAuthcode authcode){
		this.save(authcode);
		return null;
	}
	
	
	/**
	 * 获取验证码信息
	 * @param phone
	 * @return
	 */
	public SmsAuthcode getAuthcode(String phone){
		SmsAuthcode authcode = (SmsAuthcode) getByHql("from SmsAuthcode where phone=? and codeStatus=1 order by sendTime desc", phone);
		return authcode;
	}
	
	
	/**
	 * 更新发送状态
	 * @param id
	 * @return
	 */
	public SysError updateSend(String id){
		SmsAuthcode authcode = getSafeEntity(id);
		if(authcode == null){
			return null;
		}
		authcode.setCodeStatus(1);
		authcode.setSendTime(new Date());
		
		Updater<SmsAuthcode> updater = new Updater<>(authcode, "codeStatus,sendTime");
		this.update(updater);
		
		return null;
	}
	
	
	/**
	 * 更新验证状态
	 * @param id
	 * @param succeed
	 * @return
	 */
	public SysError updateAuth(String id, boolean succeed){
		SmsAuthcode authcode = getSafeEntity(id);
		if(authcode == null){
			return null;
		}
		authcode.setCodeStatus(succeed ? 2 : 3);
		authcode.setAuthTime(new Date());
		
		Updater<SmsAuthcode> updater = new Updater<>(authcode, "codeStatus,authTime");
		this.update(updater);
		
		return null;
	}
	
	
	
	
}
